package top.malaoshi.jdbc;

import java.util.*;

/**
 * 封装一行数据
 * @author mym
 *
 */
public class Record {
    public List<String> columns;
    public Map<String,Object> datas;
    public Record(){
        columns=new ArrayList<>();
        datas=new LinkedHashMap<>();
    }
    public Record(int initNum){
        columns=new ArrayList<>(initNum);
        datas=new HashMap<>(initNum);
    }
    /**
     * 放数据
     * @param column
     * @param value
     */
    public void put(String column,Object value){
        columns.add(column);
        datas.put(column, value);
    }
    /**
     * 根据索引取出列名
     * @param index
     * @return
     */
    public String getColumn(int index){
        return columns.get(index);
    }
    /**
     * 根据索引取出值
     * @param index
     * @return
     */
    public Object getValue(int index){
        String column=columns.get(index);
        return datas.get(column);
    }
    /**
     * 根据列名取出值
     * @param column
     * @return
     */
    public String getStringValue(String column){
        Object value = datas.get(column);
        return (String)value;
    }

    /**
     * 根据索引取出值
     * @param index
     * @return
     */
    public String getStringValue(int index){
        String column=columns.get(index);
        Object value = datas.get(column);
        return (String)value;
    }

    /**
     * 根据列名取出值
     * @param column
     * @return
     */
    public Long getLongValue(String column){
        Object value = datas.get(column);
        return (Long)value;
    }

    /**
     * 根据索引取出值
     * @param index
     * @return
     */
    public Long getLongValue(int index){
        String column=columns.get(index);
        Object value = datas.get(column);
        return (Long)value;
    }
    public Integer getIntValue(String column){
        Object value = datas.get(column);
        return (Integer)value;
    }
    public Integer getIntValue(int index){
        String column=columns.get(index);
        Object value = datas.get(column);
        return (Integer)value;
    }
    /**
     * 根据列名取出值
     * @param column
     * @return
     */
    public Double getDoubleValue(String column){
        Object value = datas.get(column);
        return (Double)value;
    }

    /**
     * 根据索引取出值
     * @param index
     * @return
     */
    public Double getDoubleValue(int index){
        String column=columns.get(index);
        Object value = datas.get(column);
        return (Double)value;
    }
    /**
     * 根据列名取出值
     * @param column
     * @return
     */
    public Object getValue(String column){
        return datas.get(column);
    }
    /**
     * 返回所有列名
     * @return
     */
    public String[] getColumns() {
        String[] array=new String[columns.size()];
        return columns.toArray(array);
    }
    /**
     * 返回Map，key是column，value是值
     * @return
     */
    public Map<String, Object> toMap() {
        return datas;
    }
    /**
     * 返回Map，key是column，value是值
     * key 经过 keyHandler 处理
     * @return
     */
    public Map<String, Object> toMap(KeyHandler keyHandler) {
        Map<String,Object> ret = new LinkedHashMap<>(datas.size());

        Set<Map.Entry<String, Object>> entries = datas.entrySet();
        for(Map.Entry<String, Object> item : entries){
            String key = item.getKey();
            Object value = item.getValue();

            String newKey = keyHandler.handle(key);
            ret.put(newKey, value);
        }

        return ret;
    }

    /**
     * 返回Map，key是column，value是值
     * key 经过 keyHandler 处理
     * @return
     */
    public Map<String, Object> toMap(KeyValueHandler keyValueHandler) {
        Map<String,Object> ret = new LinkedHashMap<>(datas.size());

        Set<Map.Entry<String, Object>> entries = datas.entrySet();
        for(Map.Entry<String, Object> item : entries){
            String key = item.getKey();
            Object value = item.getValue();

            String newKey = keyValueHandler.handleKey(key);
            Object newValue = keyValueHandler.handleValue(value);
            ret.put(newKey, newValue);
        }

        return ret;
    }
    /**
     * 返回所有的 数据
     * @return
     */
    public Object[] getValues(){
        int size=columns.size();
        Object[] array=new Object[size];
        for(int i=0;i<size;i++){
            String column=columns.get(i);
            array[i]=datas.get(column);
        }
        return array;
    }
    /**
     * 返回数据的数量
     * @return
     */
    public int getSize(){
        return columns.size();
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder("Record{");

        for( String column :  columns){
            ret.append(column);
            ret.append("=");
            ret.append(getValue(column));
            ret.append(",");
        }
        int len = ret.length();
        ret.delete(len-1,len);
        ret.append("}");
        return ret.toString();
    }
}
